<?php
class WishAction extends Action{

	/**
	 * 心愿单列表
	 */
	public function getWishList($activityName='', $isMemberPartake='', $status='', $startTime='', $endTime='', $start=0, $limit=25){
		$retVal = Session::init();
		
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		$cid = Session::$user['customerId'];
        $wishModel = M('activity');
        ($activityName != '') ? $where['a.activityName'] = array('like',"%{$activityName}%") : '';
        ($isMemberPartake != '') ? $where['a.isMemberPartake'] = $isMemberPartake : '';
        if($status != ''){
            if($status != '2'){
                $where['a.status'] = $status;
                $where['a.endTime'] = array('EGT',date('Y-m-d H:i:s'));
            }else{
                $where['a.endTime'] =  array('ELT',date('Y-m-d H:i:s'));
            }
        }
        ($startTime != '') ? $where['a.startTime'] = array('EGT', $startTime) : '';
        if($endTime != ''){

            if($where['a.endTime'] != ''){
                $where['a.endTime'] = array($where['a.endTime'],array('ELT', $endTime),'and');
            }else{
                $where['a.endTime'] = array('ELT', $endTime);
            }
        }
        $where["a.deleted"] = 0;
        $arr = $wishModel->alias('a')
        ->join("inner join wx_{$cid}_wish_act b On a.id = b.activId")
        ->join("left join (select group_concat(d.name) as tags,c.wishActId from  wx_{$cid}_wish_act_tags c left join wx_module_product_tag d on c.proTagId=d.id group by c.wishActId) as e on e.wishActId=b.id")
        ->where($where)->limit("{$start},{$limit}")
        ->order("a.`updateTime` DESC, a.`createTime` DESC")
        ->field("a.*,b.activId,b.id as actId,e.tags")->select();

        foreach($arr as $k => $v){
            if($v['endTime'] < date('Y-m-d H:i:s')){
                $arr[$k]['status'] = 2;
            }
        }
        if($arr){
            $count = $wishModel->alias('a')->join("wx_{$cid}_wish b On a.id = b.activId")->where($where)->count();
            return Output::instance()->objArray($arr, $count);
        }else{
            return Output::instance()->resultCode(code_success);
        }
	}
	
	/**
	 * 保存活动
	 */
	public function save($id,$activId,$bannelUrl, $pids, $wishTexts='', $shareTitle='',$sharePicUrl=''){
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
        $now = date('Y-m-d H:i:s');
        $cid = Session::$user['customerId'];
        $activeWishModel=M("wish_act");
        $addData["bannelUrl"]=$bannelUrl;
        $addData["wishTexts"]=$wishTexts;
        $addData["shareTitle"]=$shareTitle;
        $addData["sharePicUrl"]=$sharePicUrl;
        $addData["activId"]=$activId;
        $addData["updateTime"]=$now;
        if($id) {
           $result= $activeWishModel->where("id=$id")->save($addData);
           $wishActId=$id;
        } else{
           $addData["createTime"]=$now;
           $result= $activeWishModel->add($addData);
           $wishActId=$result;
           $activModel = M('activity');
           $update['moduleId'] = $result;
           $where['id'] = $activId;
           $activResult = $activModel->where($where)->save($update);
        }
        if($result) {
            $activeProModel=M("wish_act_tags");
            if($pids) {
                if($id) {
                    $activeProModel->where("wishActId='$wishActId'")->delete();
                }
                $pids = explode(',',$pids);
                foreach ($pids as $value) {
                    $addDataPro["proTagId"]=$value;
                    $addDataPro["wishActId"]=$wishActId;
                    $activeProModel->add($addDataPro);
                }
            }
            Output::instance()->resultCode(code_success);
        } else{
            Output::instance()->resultCode(code_sql_error);
        }
	}
    public function getWishById($id){
        
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }

        $cid = Session::$user['customerId'];

        $wishModel = m('wish_act');
        $where['a.id'] = $id;
        $result = $wishModel->alias('a')
        ->join("left join wx_{$cid}_wish_act_tags b On a.id = b.wishActId")
        ->field("a.*, group_concat(b.proTagId) as proIds")
        ->where($where)
        ->find();
        if ($result) {
            Output::instance()->object($result);
        } else {
            Output::instance()->resultCode(code_success);
        }
    }
    /*获取设置了心愿的用户列表*/
    public function getUserWishList($actId,$start=0, $limit=25){
        $retVal = Session::init();
        
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $cid = Session::$user['customerId'];
        $wishModel=M("wish");
        $where["a.wishActId"]=array('eq',$actId);

        $list=$wishModel->alias("a")
        ->join("left join wx_{$cid}_user_weixin_info b on b.openid=a.userName")
        ->field("a.*,b.nickname,b.headimgurl
            ,(select count(c.id) from wx_{$cid}_wish as c inner join wx_{$cid}_wish_product d on c.id=d.wishId where c.id=a.id) as num1
            ,(select count(c.id) from wx_{$cid}_wish as c inner join wx_{$cid}_wish_product d on c.id=d.wishId where d.buyUserName<>'' and c.id=a.id) as num2"
        )
        ->order('a.createTime desc')
        ->where($where)
        ->select();
        if($list){
            $count= $wishModel->alias('a')->where($where)->count();
            return Output::instance()->objArray($list, $count);
        }else{
            return Output::instance()->resultCode(code_success);
        }
    }
    /*获取心愿单详细信息*/
    public function getWishBuyList($wishId,$start=0, $limit=25){
        $retVal = Session::init();
        
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $cid = Session::$user['customerId'];
        $wishModel=M("wish");
        $where["a.id"]=array('eq',$wishId);

        $list=$wishModel->alias("a")
        ->join("inner join wx_{$cid}_wish_product b on a.id=b.wishId")
        ->join("inner join wx_module_product d on b.pId=d.id")
        ->join("left join wx_{$cid}_user_weixin_info e on e.openid=b.buyUserName")
        ->field("a.*,b.*,e.nickname,e.headimgurl,d.name,d.fixedPrice,d.discountedPrice")
        ->order('a.createTime desc')
        ->where($where)
        ->select();
        if($list){
            $count= $wishModel->alias('a')
            ->join("inner join wx_{$cid}_wish_product b on a.id=b.wishId")
            ->join("inner join wx_module_product d on b.pId=d.id")
            ->join("left join wx_{$cid}_user_weixin_info e on e.openid=a.userName")
            ->field("count(a.id)")
            ->order('a.createTime desc')
            ->where($where)->count();
            return Output::instance()->objArray($list, $count);
        }else{
            return Output::instance()->resultCode(code_success);
        }
    }
}